package android.textIt.frontend.adapter;

import java.util.List;

import android.content.Context;
import android.textIt.R;
import android.textIt.backend.dao.GroupDao;
import android.textIt.backend.model.Group;
import android.textIt.util.TextItUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class GroupAdapter extends ArrayAdapter<Group> {
	 private Context context;
	 private List<Group> items;
	 private GroupDao groupDAO;

   public GroupAdapter(Context context, List<Group> groupList) {
           super(context, R.layout.li_groups, groupList);
           this.context = context;
           this.items = groupList;
           this.groupDAO = new GroupDao(context);
   }
   
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       if (convertView == null) {
           LayoutInflater vi = 
          	 (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           convertView = vi.inflate(R.layout.li_groups, null);
       }
       Group group = items.get(position);
       TextView tvGroup = (TextView) convertView.findViewById(R.id.TxtGroupName);
       tvGroup.setText(group.getName());
       
       TextView tvId = (TextView) convertView.findViewById(R.id.TxtGroupId);
       tvId.setText(Long.toString(group.getId()));

       TextView tvMembers = (TextView) convertView.findViewById(R.id.TxtGroupMembers);
       tvMembers.setText(TextItUtil.membersToString(groupDAO.getUsersInGroup(group)));
       return convertView;
   }
   

}
